package com.zzb.framework.starter.xxljob.controller;

import com.xxl.job.admin.core.exception.XxlJobException;
import com.xxl.job.admin.core.model.XxlJobGroup;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.model.XxlJobUser;
import com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum;
import com.xxl.job.admin.core.thread.JobTriggerPoolHelper;
import com.xxl.job.admin.core.trigger.TriggerTypeEnum;
import com.xxl.job.admin.core.util.I18nUtil;
import com.xxl.job.admin.dao.XxlJobGroupDao;
import com.xxl.job.admin.dao.XxlJobInfoDao;
import com.xxl.job.admin.service.LoginService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.enums.ExecutorBlockStrategyEnum;
import com.xxl.job.core.glue.GlueTypeEnum;
import com.zzb.framework.common.BasePage;
import com.zzb.framework.common.BaseResponse;
import com.zzb.framework.starter.xxljob.form.XxlJobInfoSearchForm;
import com.zzb.framework.starter.xxljob.service.MyXxlJobService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * index controller
 * @author xuxueli 2015-12-19 16:13:16
 */
@RestController
@RequestMapping("/framework/xxljob/jobinfo")
public class MyJobInfoController {

	@Resource
	private XxlJobGroupDao xxlJobGroupDao;
	@Resource
	private MyXxlJobService myXxlJobService;
	@Resource
	private XxlJobInfoDao xxlJobInfoDao;


	@RequestMapping("/pageList")
	public BasePage<com.zzb.framework.starter.xxljob.model.XxlJobInfo> pageList(XxlJobInfoSearchForm searchForm) {
		return myXxlJobService.pageList(searchForm);
	}

	@RequestMapping("/add")
	public BaseResponse<String> add(@RequestBody XxlJobInfo jobInfo) {
		return myXxlJobService.add(jobInfo);
	}

	@RequestMapping("/update" )
	public BaseResponse<String> update(@RequestBody XxlJobInfo jobInfo) {
		return myXxlJobService.update(jobInfo);
	}

	@RequestMapping("/remove")
	public BaseResponse<String> remove(int id) {
		return myXxlJobService.remove(id);
	}

	@RequestMapping("/stop")
	public BaseResponse<String> pause(int id) {
		return myXxlJobService.stop(id);
	}

	@RequestMapping("/start")
	public BaseResponse<String> start(int id) {
		return myXxlJobService.start(id);
	}

	@RequestMapping("/trigger")
	public BaseResponse<String> triggerJob(int id, String executorParam) {
		// force cover job param
		if (executorParam == null) {
			executorParam = "";
		}

		JobTriggerPoolHelper.trigger(id, TriggerTypeEnum.MANUAL, -1, null, executorParam);
		return BaseResponse.ok();
	}

	@RequestMapping("/loadById")
	public BaseResponse<XxlJobInfo> loadById(int id){
		XxlJobInfo jobInfo = xxlJobInfoDao.loadById(id);
		return BaseResponse.content(jobInfo);
	}

	@RequestMapping("/getJobsByGroup")
	public List<XxlJobInfo> getJobsByGroup(int jobGroup){
		List<XxlJobInfo> list = xxlJobInfoDao.getJobsByGroup(jobGroup);
		return list;
	}


}
